import pojo.Account;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

//ResultSetn 常用API详解
public class jdbc_ResultSet {
    //执行DQL查询方法，ResultSet
//    public static void main(String[] args) throws Exception {
//        //1.注册驱动
////        Class.forName("com.mysql.jdbc.Driver");
//        //2.获取连接
//        String url="jdbc:mysql:///db1";
//        String username="root";
//        String password="123456";
//        Connection conn = DriverManager.getConnection(url, username, password);
//        //3.定义sql语句
//       String sql = "select * from account";
//        //4.获取sql对象
//        Statement statement = conn.createStatement();
//        //5.执行sql语句
//        ResultSet rs = statement.executeQuery(sql);
////        6.处理结果
//        while (rs.next()){
//            //7.获取数据
//            int id = rs.getInt(1);
//            String name = rs.getString(2);
//            double money = rs.getDouble(3);
//            System.out.println(id);
//            System.out.println(name);
//            System.out.println(money);
//            System.out.println("-----------------");
//        }
////        8.释放资源
//        rs.close();
//        statement.close();
//        conn.close();
//        }
    //        while (rs.next()){
//            //7.获取数据
//            int id = rs.getInt("id");
//            String name = rs.getString("name");
//            double money = rs.getDouble("money");
//            System.out.println(id);
//            System.out.println(name);
//            System.out.println(money);
//            System.out.println("-----------------");

    /**
     * 查询account账户表数据，封装为account对象
     * 1.定义一个实体类
     * 2.查询数据，封装到account对象中
     * 3.将account对象存入list集合中
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        //1.注册驱动
//        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url="jdbc:mysql:///db1";
        String username="root";
        String password="123456";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.定义sql语句
        String sql = "select * from account";
        //4.获取sql对象
        Statement statement = conn.createStatement();
        //5.执行sql语句
        ResultSet rs = statement.executeQuery(sql);
        //创建一个集合
        List<Account> list = new ArrayList<>();
//        6.处理结果
        while (rs.next()){
            Account account = new Account();
            //7.获取数据
            int id = rs.getInt(1);
            String name = rs.getString(2);
            double money = rs.getDouble(3);
            //赋值
            account.setId(id);
            account.setName(name);
            account.setMoney(money);
            list.add(account);
        }
        System.out.println(list);
//        8.释放资源
        rs.close();
        statement.close();
        conn.close();
    }
    }

